package com.hdoit.ares.quartz;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.tennetcn.common.annotation.QuartzTaskConfig;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.logical.systask.service.ISmsSendService;
import com.tennetcn.common.utils.DateUtils;

@QuartzTaskConfig(cronTrigger = "0 0 2,10,20,23 * * ?", jobTitle = "每日检查系统服务器上的网页是否可以正常连接")
public class QuartzMonitoringInformationJob implements Job{
	/*"0 0 2,10,20,23 * * ?"*/
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		
        List<String> urls = new ArrayList<String>();
		
		String homeIndex="http://www.putiyoli.net/ares/wechat/home/index";
		urls.add(homeIndex);
		String carUrl="https://m.52zzb.com/#/channelIndex?authCode=G7P1tnAdy6ts13cIXr4EDeninOpEnLht&r=000442";
		urls.add(carUrl);
		String  personCenter = "http://www.putiyoli.net/ares/wechat/cust/personCenter/index";
		urls.add(personCenter);
		String  cloudSharing = "http://www.putiyoli.net/ares/wechat/share/cloudClass";
		urls.add(cloudSharing);
		String  accidentInsu = "http://www.putiyoli.net/ares/wechat/insu/insuranceClassify/showInsu";
		urls.add(accidentInsu);
		
		for(String url:urls){
			monitoringWebPage(url);
		}
		
        
			
	}
	
	public void monitoringWebPage(String surl){
		StringBuffer errMessage = new StringBuffer("");
		System.err.println(errMessage);
		
		try { 
            URL url = new URL(surl);
            URLConnection rulConnection   = url.openConnection();
            HttpURLConnection httpUrlConnection  =  (HttpURLConnection) rulConnection;
            httpUrlConnection.setConnectTimeout(300000);
            httpUrlConnection.setReadTimeout(300000);
            httpUrlConnection.setInstanceFollowRedirects(false);
            httpUrlConnection.connect();
            
            String code = new Integer(httpUrlConnection.getResponseCode()).toString();
            String message = httpUrlConnection.getResponseMessage();
            System.err.println("getResponseCode code ="+ code);
            System.err.println("getResponseMessage message ="+ message);
            if(code.startsWith("4") || code.startsWith("5")){
          	   errMessage.append("错误的页面地址：" + surl + "\n");
          	   errMessage.append("错误状态码：" + code + "\n");
          	   errMessage.append("错误原因：" + message + "\n");
          	   sendMessage(errMessage);
            }
            System.err.println(DateUtils.getCurrent()+"连接"+surl+"正常");
       }catch(Exception ex){
      	    errMessage.append("错误的页面地址：" + surl + "\n");
      	    errMessage.append("错误原因：" + ex.getMessage() + "\n");
      	    sendMessage(errMessage);
            System.err.println(ex.getMessage());
       } 
		
	}
	
	public void sendMessage(StringBuffer errMessage){
	
		String err = "" + errMessage;
		ISmsSendService smsSendService=(ISmsSendService)CommonApplicationContextUtil.getCurrentContext().getBean(ISmsSendService.class);
		smsSendService.sendVerifyCode("16675344432", err, "192.168.200.149");
		smsSendService.sendVerifyCode("15168456433", err, "192.168.200.149");
		smsSendService.sendVerifyCode("13008862853", err, "192.168.200.149");
	 }
}


